﻿Imports System.Data.OleDb
Public Class detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'meaning, if it is a post back, then we jump out of the function and do not run the rest below

        If Not IsPostBack Then
            add.Visible = True
            success.Visible = False
        End If
        If IsPostBack Then
            success.Visible = True
            add.Visible = False
            Return
        End If
        'displays the page and cuts out code below if user is unregistered (i.e. does not log unregistered users movements)
        If Request.Params("ID") = Nothing Then
            Response.Redirect("detail.aspx?ID=1")
        End If
        If User.Identity.IsAuthenticated Then
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
            Dim sql = "INSERT INTO BooksViewedByUsers (Bookfkid,BookCommenter) VALUES (@f1,@f2)"
            Dim cmd = New OleDbCommand(sql, conn)
            conn.Open()
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            cmd.ExecuteNonQuery()

            Dim sql2 = "INSERT INTO BooksViewedByUserIds (Bookfkid,Userfkid) VALUES (@f1,@f2)"
            Dim cmd2 = New OleDbCommand(sql2, conn)
            cmd2.Parameters.AddWithValue("@f1", Request.Params("ID"))
            cmd2.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
            cmd2.ExecuteNonQuery()
        End If
        bookDetail()
    End Sub
    Sub bookDetail()
        Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        conn.Open()
        Dim cmdbook As New OleDbCommand("SELECT * FROM booklist WHERE (ID = ?) ", conn)
        cmdbook.Parameters.AddWithValue("@f1", Request.Params("ID"))
        detailView.DataSource = cmdbook.ExecuteReader()
        detailView.DataBind()
        conn.Close()
    End Sub
    Protected Sub btnAddToLibrary()
        Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        conn.Open()
        Dim cmdshelf As New OleDbCommand("INSERT INTO bookshelfjunction (UserName,BookID) VALUES(@f1,@f2) ", conn)
        cmdshelf.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmdshelf.Parameters.AddWithValue("@f2", Request.Params("ID"))
        cmdshelf.ExecuteNonQuery()
        conn.Close()
    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If User.Identity.IsAuthenticated = False Then
            Response.Redirect("~/Account/Login.aspx")
            Return
        End If
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim sql = "INSERT INTO Comments (BookCommenter,TagLine,CommentBody,BookID) VALUES (@f1,@f2,@f3,@f4)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Subject.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Message.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("detail.aspx?ID=" & Request.Params("ID"))
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim bookId = Request.Params("ID")
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        conn.Open()

        Dim newTags = tb_NewTags.Text.Split(",")

        For Each newTag As String In newTags
            Dim isTagThereCmd As New OleDbCommand("SELECT * FROM tag WHERE Tag=@f1", conn)
            isTagThereCmd.Parameters.AddWithValue("@f1", newTag)
            Dim dr = isTagThereCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagCmd As New OleDbCommand("INSERT INTO tag (Tag) VALUES (@f1)", conn)
                addTagCmd.Parameters.AddWithValue("@f1", newTag)
                addTagCmd.ExecuteNonQuery()
            End If
            Dim findNewTagIDCmd As New OleDbCommand("SELECT ID FROM tag WHERE Tag=@f1", conn)
            findNewTagIDCmd.Parameters.AddWithValue("@f1", newTag)
            Dim newTagDr = findNewTagIDCmd.ExecuteReader()
            newTagDr.Read()
            Dim newID = newTagDr.Item("ID")
            Dim makeAssociationCmd As New OleDbCommand("INSERT INTO booktagjunction (Tagfkid,Bookfkid) VALUES (@f1,@f2)", conn)
            makeAssociationCmd.Parameters.AddWithValue("@f1", newID)
            makeAssociationCmd.Parameters.AddWithValue("@f2", bookId)
            makeAssociationCmd.ExecuteNonQuery()
        Next
        For Each li As ListItem In CheckBoxList1.Items
            If (li.Selected = True) Then
                Dim sql = "INSERT INTO booktagjunction (Tagfkid,Bookfkid) VALUES (@f1,@f2)"
                Dim cmd As New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", li.Value)
                cmd.Parameters.AddWithValue("@f2", bookId)
                cmd.ExecuteNonQuery()
            End If
        Next
        conn.Close()
    End Sub


End Class
